//
//  CZWelcomeViewController.swift
//  Weibo24
//
//  Created by apple on 16/2/29.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SDWebImage

/*
    1.分析界面 -> 头像,名字,背景图片
    2.分析功能 -> 动画,页面跳转,用户名和头像

*/
class CZWelcomeViewController: UIViewController {

    override func loadView() {
        view = bgImageView
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
//         startAnimation()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
//         startAnimation()
    }
    
    //视图出现完成之后,再调用动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    private func startAnimation(){
    
        /*
            usingSpringWithDamping,弹簧的系数 0~1.0
            initialSpringVelocity,弹簧的初始速度
            枚举: 如果采用默认值,直接可以使用 []
        */
        
        // 更新约束
        self.photoImageView.snp_updateConstraints { (make) -> Void in
            
            make.bottom.equalTo(self.view.snp_bottom).offset(-kUIScreenHeight + 100)
            
            // 其他的约束不要修改.,直接复制过来
            make.centerX.equalTo(self.view.snp_centerX)
            
            //设置一个宽高
            make.size.equalTo(CGSizeMake(170/2, 170/2))
        }

        
        
        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            
            //完成约束动画的时候,我们需要调用 -- 面试的时候,经常被问到的
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                print("完成之后")
                //切换视图
                NSNotificationCenter.defaultCenter().postNotificationName(kNotificaionChangeRootVC, object: nil)
        }
        
    }
    
    //添加控件和约束
    private func setupUI(){
    
        view.addSubview(photoImageView)
        view.addSubview(nameLabel)
        
        // 设置约束
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-150)
            make.centerX.equalTo(view.snp_centerX)
            
            //设置一个宽高
            make.size.equalTo(CGSizeMake(170/2, 170/2))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(photoImageView.snp_bottom).offset(10)
            make.centerX.equalTo(view.snp_centerX)
        }
        
        //头像修改 -- placeholder
        // public convenience init?(string URLString: String)
        let url = NSURL(string: CZUserViewModel.shareInstance.model?.profile_image_url ?? "")
        if let Ourl = url{
        
            photoImageView.sd_setImageWithURL(Ourl , placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
    }
    

    //懒加载
///  背景图片
    
    private lazy var bgImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "ad_background")
        
        return img
    }()
    
///  头像
    private lazy var photoImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        img.layer.cornerRadius = 170 / 4
        // 裁剪多余的
        img.layer.masksToBounds = true
        img.layer.borderColor = UIColor.grayColor().CGColor
        
        img.layer.borderWidth = 2
        return img
    }()
    
///  欢迎归来
    private lazy var nameLabel: UILabel = {
    
        let label = UILabel()
        
        label.textAlignment = .Center // .Center 这是一个简写
        
        label.font = UIFont.systemFontOfSize(15)
        
        label.textColor = UIColor.darkGrayColor()
        
        // 添加用户名显示
        label.text = "\(CZUserViewModel.shareInstance.model?.screen_name ?? "")欢迎归来"
        
        return label
        
    }()
    
}
